/**
 * 定义了 Vuex 的一个模块，管理音乐播放的状态、属性和操作
 */
import {Icon} from "@/enums";

export default {
    state: {
        /** 音乐信息 */
        songId: "", // 音乐 ID
        songTitle: "", // 歌名
        songUrl: "", // 音乐 URL
        songPic: `/img/singerPic/default.jpg`, // 歌曲图片
        singerName: "", //  歌手名
        lyric: [], // 处理后的歌词数据

        /** 音乐播放信息 */
        isPlay: false, // 播放状态（是否播放）
        playBtnIcon: Icon.BOFANG, // 播放状态的图标（图标类型）
        volume: 0, // 音量（范围从 0 到 1）
        duration: 0, // 音乐时长（秒）
        curTime: 0, // 当前音乐的播放位置（秒）
        changeTime: 0, // 指定播放时刻（秒）
        autoNext: true, // 用于触发自动播放下一首（布尔值）

        /** 音乐列表信息 */
        currentPlayList: [], // 当前播放列表（歌曲列表）
        songDetails: null, // 单个歌单信息（歌单的详细信息）
        currentPlayIndex: -1, // 当前歌曲在歌曲列表的位置（索引，初始为 -1 代表没有歌曲）
    },
    getters: {
        // 获取对应的值
        songId: (state) => state.songId,
        songTitle: (state) => state.songTitle,
        songUrl: (state) => state.songUrl,
        songPic: (state) => state.songPic,
        singerName: (state) => state.singerName,
        lyric: (state) => state.lyric,

        isPlay: (state) => state.isPlay,
        playBtnIcon: (state) => state.playBtnIcon,
        volume: (state) => state.volume,
        duration: (state) => state.duration,
        curTime: (state) => state.curTime,
        changeTime: (state) => state.changeTime,
        autoNext: (state) => state.autoNext,

        currentPlayList: (state) => state.currentPlayList,
        songDetails: (state) => state.songDetails,
        currentPlayIndex: (state) => state.currentPlayIndex,
    },
    mutations: {
        // 更新对应的值
        setSongId: (state, songId) => {
            state.songId = songId;
        },
        setSongTitle: (state, songTitle) => {
            state.songTitle = songTitle;
        },
        setSongUrl: (state, songUrl) => {
            state.songUrl = songUrl;
        },
        setSongPic: (state, songPic) => {
            state.songPic = songPic;
        },
        setSingerName: (state, singerName) => {
            state.singerName = singerName;
        },
        setAutoNext: (state, autoNext) => {
            state.autoNext = autoNext;
        },
        setLyric: (state, lyric) => {
            state.lyric = lyric;
        },

        setIsPlay: (state, isPlay) => {
            state.isPlay = isPlay;
        },
        setPlayBtnIcon: (state, playBtnIcon) => {
            state.playBtnIcon = playBtnIcon;
        },
        setVolume: (state, volume) => {
            state.volume = volume;
        },
        setDuration: (state, duration) => {
            state.duration = duration;
        },
        setCurTime: (state, curTime) => {
            state.curTime = curTime;
        },
        setChangeTime: (state, changeTime) => {
            state.changeTime = changeTime;
        },

        setCurrentPlayList: (state, currentPlayList) => {
            state.currentPlayList = currentPlayList;
        },
        setSongDetails: (state, songDetails) => {
            state.songDetails = songDetails;
        },
        setCurrentPlayIndex: (state, currentPlayIndex) => {
            state.currentPlayIndex = currentPlayIndex;
        },
    },
    actions: {
        playMusic: ({commit}, {id, url, pic, index, songTitle, singerName, lyric, currentSongList}) => {
            // 触发多个 mutations 以更新音乐相关状态
            commit("setSongId", id);
            commit("setSongUrl", url);
            commit("setSongPic", pic);
            commit("setCurrentPlayIndex", index);
            commit("setSongTitle", songTitle);
            commit("setSingerName", singerName);
            commit("setLyric", lyric);
            commit("setCurrentPlayList", currentSongList);
        },
    },
};
